﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;//RegistryKey
using System.Net;//HttpWebClient
using System.IO;//Stream
using System.Text.RegularExpressions; //正则表达式

namespace GoatClubPlus
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        
        //CookieContainer
        CookieContainer cookie = new CookieContainer();

        private void MainForm_Load(object sender, EventArgs e)
        {
            //启动获得用户信息支线程
            getInfo.RunWorkerAsync();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://goatclub.sinaapp.com/home.php?mod=spacecp&ac=credit");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://goatclub.sinaapp.com/home.php?mod=spacecp&ac=credit&op=log");
        }

        public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            if (cookie.Count == 0)
            {
                request.CookieContainer = new CookieContainer();
                cookie = request.CookieContainer;
            }
            else
            {
                request.CookieContainer = cookie;
            }

            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public string SendDataByPost(string Url, string postDataStr, ref CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            if (cookie.Count == 0)
            {
                request.CookieContainer = new CookieContainer();
                cookie = request.CookieContainer;
            }
            else
            {
                request.CookieContainer = cookie;
            }

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public string zzbds(string start,string end,string html)
        {
            Regex rg = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return rg.Match(html).Value;
        }

        private void getInfo_DoWork(object sender, DoWorkEventArgs e)
        {
            RegistryKey Key = Registry.LocalMachine;
            RegistryKey myreg = Key.OpenSubKey("software\\GoatClub");
            string name = myreg.GetValue("Name").ToString();
            string password = myreg.GetValue("Password").ToString();
            string pw2 = "";
            System.Text.ASCIIEncoding code = new System.Text.ASCIIEncoding();
            //解密算法
            foreach (char flag in code.GetBytes(password))
            {
                int tmp = (int)flag;
                if (tmp < 50)
                {
                    tmp += 127;
                }
                tmp -= 50;
                byte[] byteArray = new byte[] { (byte)tmp };
                pw2 += code.GetString(byteArray);
            }
            //UrlEncode
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(name); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            string name2 = sb.ToString();
            string postDataStr = "username=" + name2 + "&password=" + pw2 + "&quickforward=yes&handlekey=ls";
            //Post数据到服务器
            SendDataByPost("http://goatclub.sinaapp.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1",postDataStr,ref cookie);
            string sc=SendDataByGET("http://goatclub.sinaapp.com/home.php?mod=spacecp&ac=credit","",ref cookie);
            string u_name=zzbds("title="+"\""+"访问我的空间"+"\""+">","</a></strong>",sc);
            string uid=zzbds("<div class="+"\""+"avt y"+"\""+"><a href="+"\""+"space-uid-",".html"+"\""+">",sc);
            string avatar="http://goatclub.sinaapp.com/uc_server/avatar.php?uid="+uid+"&size=middle";
            pictureBox1.ImageLocation=avatar;
            pictureBox1.LoadAsync();
            RegistryKey key = Registry.LocalMachine;
            RegistryKey software = key.OpenSubKey("software\\GoatClub", true);
            software.SetValue("Avatar", avatar);
            string jq=zzbds("<li class="+"\""+"xi1 cl"+"\""+"><em> 金钱:</em>","&nbsp; </li>",sc);
            string jf=zzbds("<em>积分:</em>","<span class=",sc);
            label2.Text=u_name;label4.Text=uid;label6.Text=jq;label8.Text=jf;
        }
    }
}
